package com.gc.tpas.service;

import com.gc.tpas.dao.QaInfoDao;
import com.gc.tpas.dao.QaSolutionDao;
import com.gc.tpas.dao.QaTypeDao;
import com.gc.tpas.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/5/7 0007.
 */
@Service
@Transactional
public class QaInfoService {
    @Autowired
    QaInfoDao qaInfoDao;
    @Autowired
    QaSolutionDao qaSolutionDao;
    @Autowired
    QaTypeDao qaTypeDao;

    public List<QaInfo> getAllQa(){
        return qaInfoDao.findAll();
    }
    public QaInfo showDetail(int id){
        return qaInfoDao.getOne(id);
    }
    public void addQa(QaInfo exInfo, QaSolution exSolution){
        qaInfoDao.save(exInfo);
        if(exInfo.getSolutionflag()==1){
            exSolution.setProvider(exInfo.getPublisher());
            exSolution.setQaInfo(exInfo);
            qaSolutionDao.save(exSolution);
        }
    }
    public List<QaType> getTypes(){
      return qaTypeDao.findAll();
    }

    public List<QaInfo> getMyQainfo(QaUser user){
        return qaInfoDao.findByPublisherOrderByCreatetimeDesc(user);
    }

    public List<QaInfo> getMySolveQainfo(QaUser user){
        return qaInfoDao.findBySolutions_MainflagAndSolutions_ProviderOrderByCreatetimeDesc(1,user);
    }

    public List<QaInfo> getMyPartakeQainfo(QaUser user){
        return qaInfoDao.findBySolutions_ProviderOrderByCreatetimeDesc(user);
    }
    public void addSolution(QaSolution exSolution){
        qaSolutionDao.save(exSolution);
    }
    public boolean existsMainSolution(int id){
        if(qaInfoDao.findByIdAndSolutions_Mainflag(id,1).size()>0){
            return true;
        }
        return false;
    }

    public QaType addType(QaType qaType){
        return qaTypeDao.save(qaType);
    }



}
